package com.wmh.baseservice.user.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wmh.baseservice.common.config.KeyConfig;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.redis.config.RedisConfig;
import com.wmh.baseservice.common.redis.utils.RedisUtil;
import com.wmh.baseservice.common.shiro.config.ShiroConfig;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.LogUtil;
import com.wmh.baseservice.common.utils.ShiroMd5Util;
import com.wmh.baseservice.common.utils.UUIDUtil;
import com.wmh.baseservice.common.wechat.ma.template.WxTemplateMsgUtils;
import com.wmh.baseservice.merchant.pojo.*;
import com.wmh.baseservice.merchant.service.*;
import com.wmh.baseservice.system.pojo.SysMessage;
import com.wmh.baseservice.system.service.SysMessageService;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.pojo.UsJoinApply;
import com.wmh.baseservice.user.mapper.UsJoinApplyMapper;
import com.wmh.baseservice.user.service.UsInfoService;
import com.wmh.baseservice.user.service.UsJoinApplyService;
import com.wmh.baseservice.common.mybatisplus.methods.CommonServiceImpl;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author wmh generator
 * @since 2022-01-04
 */
@Service
public class UsJoinApplyServiceImpl extends CommonServiceImpl<UsJoinApplyMapper, UsJoinApply> implements UsJoinApplyService {

    @Resource
    private MerchantRolePermissionsService merchantRolePermissionsService;
    @Resource
    private MerchantInfoService merchantInfoService;
    @Resource
    private UsInfoService usInfoService;
    @Resource
    private MerchantVegetablePlotService merchantVegetablePlotService;
    @Resource
    private MerchantPlotRegionService merchantPlotRegionService;
    @Resource
    private SysMessageService sysMessageService;
    @Resource
    private UsJoinApplyService usJoinApplyService;
    @Resource
    private MerchantRoleMenuService merchantRoleMenuService;
    @Resource
    private RedisUtil redisUtil;


    //子合伙人默认菜单
    public final Long[] menuIds = {108L, 98L, 102L, 99L, 111L, 110L, 104L, 109L, 103L, 100L};
    //仅拼单商家菜单
    public final Long[] onlyGroupGoodsIds = {102L,103L,104L,112L,113L,114L,132L,133L,96L,124L};

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(UsJoinApply usJoinApply) {
        JSONObject message = new JSONObject(true);
        SysMessage sysMessage = new SysMessage();
        if (usJoinApply.getStatus() == -1) {
            //拒绝
            sysMessage.setTitle("很抱歉，您的商家入驻申请被管理员拒绝了，原因是：");
            sysMessage.setContent(usJoinApply.getRefuseReason());
            sysMessage.setUsId(usJoinApply.getUsId());
            if (!sysMessageService.save(sysMessage)) {
                throw new ServiceException("审核: 发送消息错误");
            }
            message.put("phrase1", "审核未通过");
            message.put("thing2", "很遗憾你的入驻申请未通过。");
            message.put("thing4", "请进入小程序消息中心查看失败原因。");
            UsInfo usInfo = usInfoService.getByIdForUpdate(usJoinApply.getUsId());
            WxTemplateMsgUtils.miniSendRegister(usInfo.getOpenId(), message);
        } else {
            if (usJoinApply.getType() == 3) {
                //TODO 仅拼单商家
                //同意，生成商家信息，同时依然生成土地信息（方便以后开通），给予对应菜单
                //1、生成商家账号
                String code = UUIDUtil.getCode();
                //1-2随机密码
                String substring = code.substring(24);
                String salt = ShiroMd5Util.createSalt();
                String pwd = ShiroMd5Util.encodePwd(substring + KeyConfig.SHIRO_SALE, salt);
                MerchantInfo merchantInfo = new MerchantInfo().setStatus(0).setAccount(usJoinApply.getPhone()).setSalt(salt)
                        .setPassword(pwd).setName(usJoinApply.getName()).setPhone(usJoinApply.getPhone()).setUsId(usJoinApply.getUsId())
                        .setParentId(-1L).setShares(1D)
                        .setStoreAvatar(usJoinApply.getShopStore().contains("default") ? "/files/vegetable_mall_file/default/storeAvatar.png" : usJoinApply.getShopStore())
                        .setStoreName(usJoinApply.getShopName()).setType(3);
                if (!merchantInfoService.save(merchantInfo)) {
                    throw new ServiceException("审核：添加商家账号出错");
                }
                UsInfo usInfo = usInfoService.getByIdForUpdate(usJoinApply.getUsId());
                //4、消息填写
                sysMessage.setUsId(usJoinApply.getUsId());
                sysMessage.setTitle("恭喜，您的加盟申请已通过！");
                sysMessage.setContent("您已成功入驻平台，请使用电脑端登录商家后台，网址为:https://www.tdtye.com/merchant/，账号为您当前用户所绑定手机号，默认密码为：" + substring + "，可进入商家后台进行修改");
                message.put("phrase1", "审核已通过");
                message.put("thing2", "请进入小程序消息中心处查看账密");
                message.put("thing4", "账号密码可用于登录商家后台~");
                WxTemplateMsgUtils.miniSendRegister(usInfo.getOpenId(), message);
                //发送消息
                if (!sysMessageService.save(sysMessage)) {
                    throw new ServiceException("审核: 发送消息错误");
                }
                //1-3设置默认权限
                MerchantRolePermissions merchantRolePermissions = new MerchantRolePermissions().setRole("merchant").setMerchantId(merchantInfo.getId()).setType(1);
                if (!merchantRolePermissionsService.save(merchantRolePermissions)) {
                    throw new ServiceException("审核：添加商家角色权限出错");
                }
                //1-4回写角色id
                merchantInfo.setRoleId(merchantRolePermissions.getId());
                if (!merchantInfoService.updateById(merchantInfo)) {
                    throw new ServiceException("审核：回写商家角色id出错");
                }
                //1-6回写用户商家id
                usInfo = usInfoService.getByIdForUpdate(usJoinApply.getUsId());
                if (!CommUtil.checkNull(usInfo)) {
                    throw new ServiceException("审核: 找不到用户");
                }
                usInfo.setMerchantId(merchantInfo.getId());
                if (!usInfoService.updateById(usInfo)) {
                    throw new ServiceException("审核: 修改用户商家id出错");
                }
                //1-5将角色权限添加进redis队列
                String project = RedisConfig.KEY_INFO;
                String key = "role_permissions_" + merchantInfo.getId() + ShiroConfig.SHIRO_MERCHANT + project;
                try {
                    SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

                    Set<String> roles = new HashSet<>();
                    roles.add(merchantRolePermissions.getRole());
                    info.setRoles(roles);
                    MerchantRolePermissions parent = new MerchantRolePermissions().setPermissions("所有权限").setMerchantId(merchantInfo.getId()).setType(2);
                    if (!merchantRolePermissionsService.save(parent)) {
                        throw new ServiceException("审核：添加商家角色权限出错");
                    }
                    Set<String> per = new HashSet<>();
                    per.add(parent.getPermissions());
                    info.setStringPermissions(per);
                    redisUtil.set(key, info);
                } catch (Exception e) {
                    LogUtil.error("redis角色权限添加错误,key是:" + key + "。原因是:" + e.getMessage());
                }

                for (Long menuId : onlyGroupGoodsIds) {
                    MerchantRoleMenu merchantRoleMenu = new MerchantRoleMenu().setMenuId(menuId).setRoleId(merchantRolePermissions.getId()).setStatus(1);
                    if (!merchantRoleMenuService.save(merchantRoleMenu)) {
                        throw new ServiceException("审核： 添加子商家默认菜单错误");
                    }
                }

                //2、生成土地信息
                MerchantVegetablePlot merchantVegetablePlot = new MerchantVegetablePlot().setProvince(usJoinApply.getProvince()).setArea(usJoinApply.getArea()).setCity(usJoinApply.getCity())
                        .setAddress(usJoinApply.getAddress()).setLng(usJoinApply.getLon()).setLat(usJoinApply.getLat()).setPlotName(usInfo.getNick() + "的农场")
                        .setPhone(usJoinApply.getPhone()).setName(usJoinApply.getName()).setRawMerchantId(merchantInfo.getId()).setUrlId(UUIDUtil.getTimeId()).setAvatar("/files/vegetable_mall_file/default/avatar.png").setBanner("/files/vegetable_mall_file/default/plotBanner.jpg")
                        .setLabel("[]").setBusinessType(usJoinApply.getType());
                if (!merchantVegetablePlotService.save(merchantVegetablePlot)) {
                    throw new ServiceException("审核: 添加菜地信息错误");
                }
                //3、生成菜地区域信息
                MerchantPlotRegion merchantPlotRegion = new MerchantPlotRegion().setPlotId(merchantVegetablePlot.getId()).setMerchantId(merchantInfo.getId()).setName("初始区域").setLon(0.0).setWide(0.0).setType(1);
                if (!merchantPlotRegionService.save(merchantPlotRegion)) {
                    throw new ServiceException("审核: 添加菜地区域错误");
                }
            } else {
                //TODO 土地商家
                if (usJoinApply.getHasPartner() == 1) {
                    //TODO 合作商户
                    JSONArray array = JSONArray.parseArray(usJoinApply.getPartnerInfo());
                    array.forEach(item -> {
                        //提取数据
                        JSONObject object = JSONObject.parseObject(item.toString());
                        Long usId = object.getLong("usId");
                        String phone = object.getString("phone");
                        String name = object.getString("name");
                        Double shares = object.getDouble("shares");
                        MerchantInfo merchantInfo;
                        MerchantRolePermissions merchantRolePermissions = null;
                        UsInfo usInfo = usInfoService.getByIdForUpdate(usId);
                        if (CommUtil.checkNull(usInfo.getMerchantId())) {
                            merchantInfo = merchantInfoService.getByIdForUpdate(usInfo.getMerchantId());
                            if (!CommUtil.checkNull(merchantInfo)) {
                                throw new ServiceException();
                            }
                            //设置商家类型
                            merchantInfo.setType(1);
                            merchantInfo.setStatus(0);
                            if (!usId.equals(usJoinApply.getUsId())) {
                                //设置父商家
                                merchantInfo.setParentId(usJoinApply.getUsId());
                            }
                            if (!merchantInfoService.updateById(merchantInfo)) {
                                throw new ServiceException();
                            }
                            //查询角色
                            merchantRolePermissions = merchantRolePermissionsService.getOneForUpdate(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.ROLE, "merchant")
                                    .eq(MerchantRolePermissions.MERCHANT_ID, merchantInfo.getId()).eq(MerchantRolePermissions.TYPE, 1));

                            if (!CommUtil.checkNull(merchantRolePermissions)) {
                                throw new ServiceException();
                            }
                            //删除redisKey
                            String project = RedisConfig.KEY_INFO;
                            redisUtil.del("role_permissions_" + merchantInfo.getId() + ShiroConfig.SHIRO_MERCHANT + project);
                            //删除之前的菜单
                            for (Long menuId : menuIds) {
                                if (!merchantRoleMenuService.remove(new QueryWrapper<MerchantRoleMenu>().eq(MerchantRoleMenu.MENU_ID, menuId)
                                        .eq(MerchantRoleMenu.ROLE_ID, merchantRolePermissions.getId()).eq(MerchantRoleMenu.STATUS, 1))) {
                                    throw new ServiceException();
                                }
                            }
                            //删除之前的角色和权限
                            if (!merchantRolePermissionsService.remove(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.MERCHANT_ID, merchantInfo.getId()))) {
                                throw new ServiceException();
                            }
                        } else {
                            //1、生成商家账号
                            String code = UUIDUtil.getCode();
                            //1-2随机密码
                            String substring = code.substring(24);
                            String salt = ShiroMd5Util.createSalt();
                            String pwd = ShiroMd5Util.encodePwd(substring + KeyConfig.SHIRO_SALE, salt);
                            merchantInfo = new MerchantInfo().setStatus(0).setAccount(phone).setSalt(salt)
                                    .setPassword(pwd).setName(name).setPhone(phone).setUsId(usId).setShares(shares).setType(1);
                            //1-3申请人为主商家，其他合伙人为子商家
                            if (usId.equals(usJoinApply.getUsId())) {
                                merchantInfo.setParentId(-1L);
                                merchantInfo.setStoreAvatar("/files/vegetable_mall_file/default/storeAvatar.png");
                                merchantInfo.setStoreName("默认店铺名称");
                            } else {
                                merchantInfo.setParentId(usJoinApply.getUsId());
                            }
                            if (!merchantInfoService.save(merchantInfo)) {
                                throw new ServiceException("审核：添加商家账号出错");
                            }
                            //消息填写
                            sysMessage.setUsId(usId);
                            sysMessage.setTitle("恭喜，加盟申请已通过！");
                            sysMessage.setContent("您已成功入驻平台，请使用电脑端登录商家后台，网址为:https://www.tdtye.com/merchant/，账号为您当前用户所绑定手机号，默认密码为：" + substring + "，可进入商家后台进行修改");
                            message.put("phrase1", "审核已通过");
                            message.put("thing2", "请进入小程序消息中心处查看账密");
                            message.put("thing4", "账号密码可用于登录电脑端商家后台~");
                            WxTemplateMsgUtils.miniSendRegister(usInfo.getOpenId(), message);
                            //发送消息
                            if (!sysMessageService.save(sysMessage)) {
                                throw new ServiceException("审核: 发送消息错误");
                            }

                            //1-3设置默认角色
                            merchantRolePermissions = new MerchantRolePermissions().setRole("merchant").setMerchantId(merchantInfo.getId()).setType(1);

                            if (!merchantRolePermissionsService.save(merchantRolePermissions)) {
                                throw new ServiceException("审核：添加商家角色权限出错");
                            }
                            //1-4回写角色id
                            merchantInfo.setRoleId(merchantRolePermissions.getId());
                            if (!merchantInfoService.updateById(merchantInfo)) {
                                throw new ServiceException("审核：回写商家角色id出错");
                            }
                            //1-6回写用户商家id
                            usInfo.setMerchantId(merchantInfo.getId());
                            if (!usInfoService.updateById(usInfo)) {
                                throw new ServiceException("审核: 修改用户商家id出错");
                            }
                        }
                        //1-5将角色权限添加进redis队列
                        String project = RedisConfig.KEY_INFO;
                        String key = "role_permissions_" + merchantInfo.getId() + ShiroConfig.SHIRO_MERCHANT + project;
                        try {
                            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
                            Set<String> roles = new HashSet<>();
                            roles.add(merchantRolePermissions.getRole());
                            info.setRoles(roles);
                            if (usId.equals(usJoinApply.getUsId())) {
                                //设置父级权限
                                MerchantRolePermissions parent = new MerchantRolePermissions().setPermissions("所有权限").setMerchantId(merchantInfo.getId()).setType(2);
                                if (!merchantRolePermissionsService.save(parent)) {
                                    throw new ServiceException("审核：添加商家角色权限出错");
                                }
                                Set<String> per = new HashSet<>();
                                per.add(parent.getPermissions());
                                info.setStringPermissions(per);
                            } else {
                                //设置默认菜单
                                for (Long menuId : menuIds) {
                                    MerchantRoleMenu merchantRoleMenu = new MerchantRoleMenu().setMenuId(menuId).setRoleId(merchantRolePermissions.getId()).setStatus(1);
                                    if (!merchantRoleMenuService.save(merchantRoleMenu)) {
                                        throw new ServiceException("审核： 添加子商家默认菜单错误");
                                    }
                                }
                                //设置子级默认权限
                                Set<String> per = new HashSet<>();
                                QueryWrapper<MerchantRolePermissions> queryWrapper = new QueryWrapper<MerchantRolePermissions>().isNull(MerchantRolePermissions.MERCHANT_ID).isNull(MerchantRolePermissions.ROLE).eq(MerchantRolePermissions.TYPE, 2).ne(MerchantRolePermissions.PERMISSIONS, "所有权限");
                                List<MerchantRolePermissions> rolePermissions = merchantRolePermissionsService.listForUpdate(queryWrapper);
                                rolePermissions.forEach(itemPer -> {
                                    MerchantRolePermissions son = new MerchantRolePermissions().setPermissions(itemPer.getPermissions()).setMerchantId(merchantInfo.getId()).setType(2);
                                    if (!merchantRolePermissionsService.save(son)) {
                                        throw new ServiceException("审核：添加子商家默认权限出错");
                                    }
                                    per.add(itemPer.getPermissions());
                                });
                                info.setStringPermissions(per);
                            }
                            redisUtil.set(key, info);
                        } catch (Exception e) {
                            LogUtil.error("redis角色权限添加错误,key是:" + key + "。原因是:" + e.getMessage());
                        }

                        //TODO 生成菜地和区域信息，这都是属于主商家的，进行判断
                        //2、生成土地信息
                        if (usId.equals(usJoinApply.getUsId())) {
                            MerchantVegetablePlot merchantVegetablePlot = new MerchantVegetablePlot().setProvince(usJoinApply.getProvince()).setArea(usJoinApply.getArea()).setCity(usJoinApply.getCity())
                                    .setAddress(usJoinApply.getAddress()).setLng(usJoinApply.getLon()).setLat(usJoinApply.getLat()).setPlotName(usInfo.getNick() + "的农场").setRawMerchantId(merchantInfo.getId())
                                    .setPhone(usJoinApply.getPhone()).setName(usJoinApply.getName()).setUrlId(UUIDUtil.getTimeId()).setAvatar("/files/vegetable_mall_file/default/avatar.png").setBanner("/files/vegetable_mall_file/default/plotBanner.jpg")
                                    .setLabel("[]").setBusinessType(usJoinApply.getType());
                            if (!merchantVegetablePlotService.save(merchantVegetablePlot)) {
                                throw new ServiceException("审核: 添加菜地信息错误");
                            }
                            //3、生成菜地区域信息
                            MerchantPlotRegion merchantPlotRegion = new MerchantPlotRegion().setPlotId(merchantVegetablePlot.getId()).setMerchantId(merchantInfo.getId()).setName("初始区域").setLon(0.0).setWide(0.0).setType(1);
                            if (!merchantPlotRegionService.save(merchantPlotRegion)) {
                                throw new ServiceException("审核: 添加菜地区域错误");
                            }
                        }
                    });
                } else {
                    UsInfo usInfo = usInfoService.getByIdForUpdate(usJoinApply.getUsId());
                    MerchantInfo merchantInfo;
                    MerchantRolePermissions merchantRolePermissions;
                    if (CommUtil.checkNull(usInfo.getMerchantId())) {
                        merchantInfo = merchantInfoService.getByIdForUpdate(usInfo.getMerchantId());
                        if (!CommUtil.checkNull(merchantInfo)) {
                            throw new ServiceException();
                        }
                        //设置商家类型
                        merchantInfo.setType(1);
                        merchantInfo.setStatus(0);
                        if (!merchantInfoService.updateById(merchantInfo)) {
                            throw new ServiceException();
                        }
                        //查询角色
                        merchantRolePermissions = merchantRolePermissionsService.getOneForUpdate(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.ROLE, "merchant")
                                .eq(MerchantRolePermissions.MERCHANT_ID, merchantInfo.getId()).eq(MerchantRolePermissions.TYPE, 1));

                        if (!CommUtil.checkNull(merchantRolePermissions)) {
                            throw new ServiceException();
                        }
                        //删除redisKey
                        String project = RedisConfig.KEY_INFO;
                        redisUtil.del("role_permissions_" + merchantInfo.getId() + ShiroConfig.SHIRO_MERCHANT + project);
                        //删除之前的菜单
                        for (Long menuId : menuIds) {
                            if (!merchantRoleMenuService.remove(new QueryWrapper<MerchantRoleMenu>().eq(MerchantRoleMenu.MENU_ID, menuId)
                                    .eq(MerchantRoleMenu.ROLE_ID, merchantRolePermissions.getId()).eq(MerchantRoleMenu.STATUS, 1))) {
                                throw new ServiceException();
                            }
                        }
                        //删除之前的角色和权限
                        if (!merchantRolePermissionsService.remove(new QueryWrapper<MerchantRolePermissions>().eq(MerchantRolePermissions.MERCHANT_ID, merchantInfo.getId()))) {
                            throw new ServiceException();
                        }
                    } else {
                        //TODO 个体商户
                        //1、生成商家账号
                        String code = UUIDUtil.getCode();
                        //1-2随机密码
                        String substring = code.substring(24);
                        String salt = ShiroMd5Util.createSalt();
                        String pwd = ShiroMd5Util.encodePwd(substring + KeyConfig.SHIRO_SALE, salt);
                        merchantInfo = new MerchantInfo().setStatus(0).setAccount(usJoinApply.getPhone()).setSalt(salt)
                                .setPassword(pwd).setName(usJoinApply.getName()).setPhone(usJoinApply.getPhone()).setUsId(usJoinApply.getUsId())
                                .setParentId(-1L).setShares(1D).setStoreAvatar("/files/vegetable_mall_file/default/storeAvatar.png").setStoreName("默认店铺名称").setType(1);
                        if (!merchantInfoService.save(merchantInfo)) {
                            throw new ServiceException("审核：添加商家账号出错");
                        }
                        //4、消息填写
                        sysMessage.setUsId(usJoinApply.getUsId());
                        sysMessage.setTitle("恭喜，您的加盟申请已通过！");
                        sysMessage.setContent("您已成功入驻平台，请使用电脑端登录商家后台，网址为:https://www.tdtye.com/merchant/，账号为您当前用户所绑定手机号，默认密码为：" + substring + "，可进入商家后台进行修改");
                        message.put("phrase1", "审核已通过");
                        message.put("thing2", "请进入小程序消息中心处查看账密");
                        message.put("thing4", "账号密码可用于登录电脑端商家后台~");
                        WxTemplateMsgUtils.miniSendRegister(usInfo.getOpenId(), message);
                        //发送消息
                        if (!sysMessageService.save(sysMessage)) {
                            throw new ServiceException("审核: 发送消息错误");
                        }
                        //1-3设置默认权限
                        merchantRolePermissions = new MerchantRolePermissions().setRole("merchant").setMerchantId(merchantInfo.getId()).setType(1);
                        if (!merchantRolePermissionsService.save(merchantRolePermissions)) {
                            throw new ServiceException("审核：添加商家角色权限出错");
                        }
                        //1-4回写角色id
                        merchantInfo.setRoleId(merchantRolePermissions.getId());
                        if (!merchantInfoService.updateById(merchantInfo)) {
                            throw new ServiceException("审核：回写商家角色id出错");
                        }
                        //1-6回写用户商家id
                        usInfo = usInfoService.getByIdForUpdate(usJoinApply.getUsId());
                        if (!CommUtil.checkNull(usInfo)) {
                            throw new ServiceException("审核: 找不到用户");
                        }
                        usInfo.setMerchantId(merchantInfo.getId());
                        if (!usInfoService.updateById(usInfo)) {
                            throw new ServiceException("审核: 修改用户商家id出错");
                        }
                        //1-5将角色权限添加进redis队列
                        String project = RedisConfig.KEY_INFO;
                        String key = "role_permissions_" + merchantInfo.getId() + ShiroConfig.SHIRO_MERCHANT + project;
                        try {
                            SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

                            Set<String> roles = new HashSet<>();
                            roles.add(merchantRolePermissions.getRole());
                            info.setRoles(roles);
                            MerchantRolePermissions parent = new MerchantRolePermissions().setPermissions("所有权限").setMerchantId(merchantInfo.getId()).setType(2);
                            if (!merchantRolePermissionsService.save(parent)) {
                                throw new ServiceException("审核：添加商家角色权限出错");
                            }
                            Set<String> per = new HashSet<>();
                            per.add(parent.getPermissions());
                            info.setStringPermissions(per);
                            redisUtil.set(key, info);
                        } catch (Exception e) {
                            LogUtil.error("redis角色权限添加错误,key是:" + key + "。原因是:" + e.getMessage());
                        }
                    }

                    //2、生成土地信息
                    MerchantVegetablePlot merchantVegetablePlot = new MerchantVegetablePlot().setProvince(usJoinApply.getProvince()).setArea(usJoinApply.getArea()).setCity(usJoinApply.getCity())
                            .setAddress(usJoinApply.getAddress()).setLng(usJoinApply.getLon()).setLat(usJoinApply.getLat()).setPlotName(usInfo.getNick() + "的农场")
                            .setPhone(usJoinApply.getPhone()).setName(usJoinApply.getName()).setRawMerchantId(merchantInfo.getId()).setUrlId(UUIDUtil.getTimeId()).setAvatar("/files/vegetable_mall_file/default/avatar.png").setBanner("/files/vegetable_mall_file/default/plotBanner.jpg")
                            .setLabel("[]").setBusinessType(usJoinApply.getType());
                    if (!merchantVegetablePlotService.save(merchantVegetablePlot)) {
                        throw new ServiceException("审核: 添加菜地信息错误");
                    }
                    //3、生成菜地区域信息
                    MerchantPlotRegion merchantPlotRegion = new MerchantPlotRegion().setPlotId(merchantVegetablePlot.getId()).setMerchantId(merchantInfo.getId()).setName("初始区域").setLon(0.0).setWide(0.0).setType(1);
                    if (!merchantPlotRegionService.save(merchantPlotRegion)) {
                        throw new ServiceException("审核: 添加菜地区域错误");
                    }
                }
            }
        }
        if (!usJoinApplyService.updateById(usJoinApply)) {
            throw new ServiceException("审核: 修改审核单错误");
        }
    }
}
